package com.orangelife.mobile.parkingcard.biz;

import android.os.Handler;

import com.android.volley.Request;
import com.curry.android.http.HttpUtil;
import com.curry.android.http.OrangeDataHandler;
import com.curry.android.http.OrangeErrorHandler;
import com.curry.android.http.VolleyHttpUtil;
import com.google.gson.reflect.TypeToken;
import com.orangelife.mobile.app.application.CurryApplication;
import com.orangelife.mobile.common.biz.JSONRequest;
import com.orangelife.mobile.constants.ParkingCardURL;
import com.orangelife.mobile.parkingcard.bean.ParkingListBean;
import com.orangelife.mobile.parkingcard.bean.ParkingUsersListBean;
import com.orangelife.mobile.util.JsonLocalFileUtil;

import org.json.JSONObject;

import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 停车位的服务端API
 * <p/>
 * Created by HeYuCan on 2016/7/5.
 *
 * @author hyc
 * @since 3.8
 */
public class ParkingSpaceHttpBiz {


    /** 使用权转让 */
    public static void addUser(String name, String phone, String startTime, String endTime, String linkAptHouseholderID, VolleyHttpUtil.ResponseCallBack callback) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("phone", phone);
        map.put("name", name);
        map.put("startTime", startTime);
        map.put("endTime", endTime);
        map.put("linkAptHouseholderID", linkAptHouseholderID);
        VolleyHttpUtil.getInstance().postWithCallback(ParkingCardURL.PARKING_SPACE_ADD_USER, map, callback);
    }


    public static void requestParkingSpaceList(String ownersId, VolleyHttpUtil.ResponseCallBack callback) {
        HashMap<String, String> map = new HashMap<>();
        map.put("ownersID", ownersId);
        VolleyHttpUtil.getInstance().getWithCallback(ParkingCardURL.PARKING_SPACE_LIST, map, callback);

//                String json = JsonLocalFileUtil.readFile(CurryApplication.getInstance().getContext(), "ParkingSpaceList.json");
//        try {
//            callback.handleResponse(new JSONObject(json), 0);
//        } catch (JSONException e) {
//            e.printStackTrace();
//        }
    }




    /**VolleyHttpUtil在onResume时会可能出现无法返回请求的数据*/
    public static void requestParkingSpaceList1(String ownersId, Handler handler) {
        Map<String, Object> map = new HashMap<>();
        map.put("ownersID", ownersId);
        Map<String, Object> rootMap = new HashMap<>();
        rootMap.put("map",map);
        rootMap.put("url",ParkingCardURL.PARKING_SPACE_LIST);
        rootMap.put("wat",1);
//        VolleyHttpUtil.getInstance().getWithCallback(ParkingCardURL.PARKING_SPACE_LIST, map, callback);
//        HttpUtil.get(ParkingCardURL.PARKING_SPACE_LIST,map,)
        JSONRequest.getInstance().sendRequest(JSONRequest.GET,rootMap,handler,HttpUtil.ACTION_URI);
    }



    public static void requestParkingSpaceUsersList(String apartmentID, int pageSize, int pageIndex, VolleyHttpUtil.ResponseCallBack callback) {
        HashMap<String, String> map = new HashMap<>();
        map.put("apartmentID", String.valueOf(apartmentID));
        map.put("pageIndex", String.valueOf(pageIndex));
        map.put("pageSize", String.valueOf(pageSize));
        VolleyHttpUtil.getInstance().getWithCallback(ParkingCardURL.PARKING_SPACE_USERS_LIST, map, callback);

//        String json = JsonLocalFileUtil.readFile(CurryApplication.getInstance().getContext(),"ParkingSpaceUsersList.json" );
//        try {
//            callback.handleResponse(new JSONObject(json), 0);
//        } catch (JSONException e) {
//            e.printStackTrace();
//        }
    }

    public static void requestParkingSpaceUsersList1(String apartmentID, Handler handler) {
        Map<String, Object> map = new HashMap<>();
        map.put("apartmentID", apartmentID);
        Map<String, Object> rootMap = new HashMap<>();
        rootMap.put("map",map);
        rootMap.put("url",ParkingCardURL.PARKING_SPACE_USERS_LIST);
        rootMap.put("wat",1);
//        VolleyHttpUtil.getInstance().getWithCallback(ParkingCardURL.PARKING_SPACE_LIST, map, callback);
//        HttpUtil.get(ParkingCardURL.PARKING_SPACE_LIST,map,)
        JSONRequest.getInstance().sendRequest(JSONRequest.GET,rootMap,handler,HttpUtil.ACTION_URI);
    }

    /**
     * 删除车位使用人,即删除授权
     */
    public static void deleteAuthorization(String linkAptHouseholderID, VolleyHttpUtil.ResponseCallBack callback) {
        HashMap<String, String> map = new HashMap<>();
        map.put("linkAptHouseholderID", linkAptHouseholderID);
        VolleyHttpUtil.getInstance().delWithCallback(ParkingCardURL.PARKING_SPACE_USER_DELETE, map, callback);
    }


    /**
     * 终止授权的请求
     */
    public static void terminateAuthorization(String linkAptHouseholderID, VolleyHttpUtil.ResponseCallBack callback) {
        HashMap<String, String> map = new HashMap<>();
        map.put("linkAptHouseholderID", linkAptHouseholderID);

        VolleyHttpUtil.getInstance().getWithCallback(ParkingCardURL.PARKING_SPACE_TERMINATE_AUTHORIZATION, map, callback);
    }


    public static List<ParkingListBean> handleParkingSpaceList(JSONObject response) {
        if (OrangeErrorHandler.getInstance().isSuccess(response)) {
            List<ParkingListBean> list = OrangeDataHandler.getListFromJsonWithPageEntity(response, new TypeToken<List<ParkingListBean>>() {
            }.getType(), null, null);
            return list;
        }
        return null;
    }

    public static List<ParkingUsersListBean> handleParkingSpaceUsersList(JSONObject response) {
        if (OrangeErrorHandler.getInstance().isSuccess(response)) {
            List<ParkingUsersListBean> list = OrangeDataHandler.getListFromJsonWithPageEntity(response, new TypeToken<List<ParkingUsersListBean>>() {
            }.getType(), null, null);
            return list;
        }
        return null;
    }

}
